package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.user.model.vo.response.RespReadAndPraise;
import com.quanyan.user.service.UserPushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by zhang on 16/12/5.
 */
@RestController
@RequestMapping(value = "/api/user/push", method = RequestMethod.POST)
public class UserPushController {

    private static Logger logger = LoggerFactory.getLogger(UserPushController.class);

    @Autowired
    private UserPushService userPushService;

    /**
     * 点赞和阅读
     *
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("readAndPraise")
    public APIResponse<RespReadAndPraise> readAndPraise(@RequestBody Map map, HttpServletRequest request) {
        try{
            Integer uid = (Integer) request.getAttribute("uid");
            //Integer uid = (Integer) map.get("uid");
            Object objType = map.get("type");
            Object objArticleId = map.get("articleId");
            if(objType == null || objArticleId == null){
                return APIResponse.returnFail("请输入有效参数!");
            }
            Integer type = Integer.parseInt(objType.toString());
            Integer articleId = Integer.parseInt(objArticleId.toString());

            if(uid == null && type == 1){
                uid = 0;
            }

            if(uid == null || (type != 1 && type != 2) || articleId < 0){
                return APIResponse.returnFail("请输入有效参数!");
            }

            return userPushService.readAndPraise(uid, type, articleId);
        } catch(Exception e){
            logger.error("{}", e);
            return APIResponse.returnFail("服务器异常!");
        }
    }
}
